var videoPlayer;
pageWidth = 1011;
rotateWidth = 2022;
var developersTimerId;
var developersMenuIterationFreeze = 1;
advertisersFlag = 0;
advertisersMenuCounter = 0;
var desktopMinWidth = 1024;

var RecaptchaOptions = {
    theme: 'clean',
};
function errorMessage() {
    if ($('#imei').val().length < 10) {
        $("#imie-code").show();
        return false;
    }
    return true;
}

function submitContactUs() {
    var validatesFlag = 1;

    if ($('#name').val().length === 0) {
        $('#nameError').show();
        validatesFlag = 0;
    }

    if ($("#contact-us-select-value").text() == "*Advertiser Type") {
        $('#dropdownerror').show();
        validatesFlag = 0;
    }


    filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if ($('#email').val().length === 0 || !(filter.test($('#email').val()))) {
        $('#emailError').show();
        validatesFlag = 0;
    }

    if (validatesFlag === 0)
        return false;


    $.ajax({
        url: "/handlers/mailhandler.ashx",

        data: { FullName: $('#name').val(), Company: $('#company').val(), Email: $('#email').val(), Type: $("#contact-us-select-value").text(), Message: $("#message").val() },
        cache: false,
        traditional: true,
        beforeSend: function () {
            $('#submit-button').hide();
        },
        type: "POST",
        success: function (data) {
            $('#nameError,#dropdownerror,#emailError,').hide();
            if (data === "1") {
                $('#contact').modal('hide');
                $('#contact-thanks').modal('show');
                $('#name').val("");
                $('#email').val("");
                $('#company').val("");
                $("#contact-us-select-value").text("Please select");
                $("#message").val("");
                $('#submit-button').show();
            }
            else if (data === "2")
                $('#contact-err').modal('show');
            $('#submit-button').show();
        }
    });

}
function startAdvertise() {
    flag = 1;
    flag2 = 1;
    counter = 1;
    advertisersFlag = 1;
    advertisersMenuCounter = 0;
    }
function calculateDimension() {
    //alert("c1")
	var win_w = jQuery(window).width() - 20;
    var win_h = jQuery(window).height() - 120;
    if (win_w < 951) win_w = 951;
    //alert("c2")
    //if (win_h < 700) win_h = 700;
    jQuery('.rotate, #developers, #home, #collaborate').css("width", win_w + "px");
    jQuery('.rotate, #developers, #home, #collaborate').css("height", win_h + "px");

    //alert("in calculate dimension")
    if ((win_w / 3) >= 317) {
        //alert("c4")
        jQuery('.hand').css("width", win_w / 3 + "px");
        jQuery('.hand').css("height", win_w / 3 * 1.45 + "px");
        bottom = win_w * 42.5 / 100;
        left = win_w * 6.295 / 100;
        left_box = win_w / 4 + 335;
		jQuery('.hand-image-replacedd').css({
			"bottom": bottom + "px",
			"left": left + "px",
			"display": "inline !important"			
		});


        $('.play-button,.play-button-advertisers').css("width", win_w / 12 + "px");
        jQuery('#box0').css("left", left_box + "px");
    }
}

jQuery(document).ready(function () {
	
	//checkDeviceResolution(); // Check device resolution and redirect to mobile if less than minimum desktop width

    $("#developer-header").click(function () {
        animateDevelopers("developers");
    });
    $("#collaborate-header").click(function () {
        animateCollborate();
    });
    var www = window.location.href;
    if (www.indexOf('opt.html') !== -1 || www.indexOf('opt-out.html') !== -1) {
        window.location = "opt.aspx";
    }
    // Prepare
    var History = window.History; // Note: We are using a capital H instead of a lower h
    if (!History.enabled) {
        // History.js is disabled for this browser.
        // This is because we can optionally choose to support HTML4 browsers or not.
        return false;
    }

    // Bind to StateChange Event
    History.Adapter.bind(window, 'statechange', function () { // Note: We are using statechange instead of popstate
        var State = History.getState(); // Note: We are using History.getState() instead of event.state
        if (State.data.state === 1) {
            animateCollborate();
        }
        else if (State.data.state === 2) {
            animateDevelopers();
        }
        else {
            animateHome();
        }

    });

    var img = new Image();



    $('#developers .info').tooltip();

    $(".social li a img").hover(function () {

        socialId = $(this).attr('id');
        $(this).addClass(socialId);
        $("#" + socialId + "2").show();
    }, function () {
        $(this).removeClass(socialId);
        $("#facebook2").hide();
        $("#twitter2").hide();
        $("#linkedin2").hide();
    });



    jQuery('.team img').hover(function () {

        var pictureId = parseInt($(this).attr("id")[3]);
        var left = 45 + pictureId * 140
        $(".team .about-us-arrow").css("left", left + "px")
        $(".user-info").hide();
        $("#details" + pictureId).show()

    });



    jQuery('#home .box').hover(function () {

        var me = this;
        jQuery(this).addClass('hover');
        var currentId = $(this).attr('id');
        idNumber = parseInt(currentId[3]);
        if (idNumber === 1)
            $('#arrow1 img').show();
        $('#arrow' + idNumber).stop(false, true).fadeIn('slow');
        idNumber = 1 - idNumber;
        $('#arrow' + idNumber).stop(false, true).fadeOut('slow');
        jQuery("#box" + idNumber).removeClass('hover');
        if (jQuery('#home .hand img').last().attr('src') != jQuery(this).find('a').attr('rel')) {
            jQuery('#home .hand img').last().stop(false, true).fadeOut('fast', function () {
                jQuery('#home .hand img').last().attr('src', jQuery(me).find('a').attr('rel'));
            });
            jQuery('#home .hand img').last().stop(false, true).fadeIn('fast');
        }
    });

    flag = 0;
    counter = 1;
    i = 0;
    ad_image_rate = 4000;

  
    window.setInterval(function () {
      
       
        if (advertisersFlag === 1) {
            var $advertisersLis = jQuery('#collaborate .select li');
            jQuery('#collaborate .select li').removeClass("hover");
            jQuery('#ad_img').fadeOut("slow", function () {


                advertisersMenuCounter = ((advertisersMenuCounter + 1) % $advertisersLis.length);


                jQuery('.advertisers-phone-background').attr("src", jQuery('#div_' + (advertisersMenuCounter)).attr('rel-url'));
                jQuery('#div_' + (advertisersMenuCounter)).attr('index', advertisersMenuCounter);

                if (advertisersFlag == 1)//check again if the there is not hover
                {
                    hoverAdvertisersItem(jQuery('#collaborate .select li:eq(' + advertisersMenuCounter + ')'));

                }

            });
        }
    }, ad_image_rate);


    
    // Advertisers Script
    jQuery('#collaborate .select li').hover(
        function () {
            if (!$(this).hasClass('hover'))
            {
                hoverAdvertisersItem(this)
            }
            else // pause
            {
                advertisersFlag = false;
            }
        }
    , function () {
        flag = 1;
        advertisersFlag = 1;
    });

    jQuery('.advertisers-phone-background,#collaborate .hand,#collaborate .play-button-advertisers,#collaborate li').hover(function () {
        advertisersFlag = 0;

    }).mouseleave(function () {
        advertisersFlag = 1;
    });

    

    var hoverAdvertisersItem = function (me) {

        
        advertisersMenuCounter = $(me).attr('index');
        var currId = jQuery(this).attr('id');
        jQuery('#collaborate .select li').removeClass("hover");
        jQuery(me).addClass('hover');
        jQuery('#ad_img').fadeOut("fast", function () {
            if ($(me).attr('is-video-item') == '1') {
                $('.play-button-advertisers').show("fast");

                $('img.hand-image-advertisers ,.play-button-advertisers').unbind('click');
                $('img.hand-image-advertisers,.play-button-advertisers').bind('click', function (e) {
                    showVideoPlayerAdvertisers($(me).attr('video-url'));
                });

            }
            else {
                $('.play-button-advertisers').hide();
                $('img.hand-image-advertisers ,.play-button-advertisers').unbind('click');

            }
            jQuery('#ad_img').attr("src", $(me).attr('rel-url'));
            jQuery('#ad_img').fadeIn("fast");
        });


    };


    hoverAdvertisersItem($('#collaborate .select li:first'));

    //*********** Start Developers Menu ***************/
    //init Hover
	var sender = $('#developers .dev-box .select li[item-index="0"]').addClass('hover');
    setPhoneBackgroundImage(sender);
    $('#developers .dev-box .select li').one('mouseenter', function () {
        $('#developers .dev-box .select li').removeClass('hover');
        $(this).addClass('hover');
        developersMenuIterationFreeze = 1;
        setPhoneBackgroundImage(this);

        $(this).bind('mouseleave', function () {
            developersMenuIterationFreeze = 0;
            $(this).one('mouseenter', function () {
                $('#developers .dev-box .select li').removeClass('hover');
                $(this).addClass('hover');
                developersMenuIterationFreeze = 1;
                setPhoneBackgroundImage(this);
            });
        });

       
        $('.hand-image-developers,.play-button').one('mouseenter', function () {
            developersMenuIterationFreeze = 1;
            $(this).bind('mouseleave', function () {
                developersMenuIterationFreeze = 0;
                $(this).one('mouseenter', function () {
                    developersMenuIterationFreeze = 1;
                });

            });
        });
         

    });


    initDevelopersSection();



    //******************** End Developers Menu ************/

    jQuery('.faq li').live('click', function () {
        if (jQuery(this).hasClass('open')) {
            jQuery(this).removeClass('open');
        } else {
            jQuery(this).addClass('open');
        }
        return true;
    });




    if (jQuery('#contact-us').size() > 0) {

        jQuery(window).resize(function () {
            calculateDimension();
        });

        setTimeout(function () {

            calculateDimension();

        });
    }

    if (jQuery('#home').size() > 0) {

        jQuery(window).resize(function () {
            calculateDimension();
        });

        setTimeout(function () {

            calculateDimension();

            jQuery('#developers').show().css('position', 'absolute').css('left', -rotateWidth + 'px').css('top', '0');

            jQuery('#collaborate').show().css('position', 'absolute').css('left', rotateWidth + 'px').css('top', '0');

            jQuery('#home').css('position', 'absolute').css('left', '0').css('top', '0');


            var www = window.location.href;
            if (www.indexOf('#advertisers') !== -1 || www.indexOf('?advertisers') !== -1) {
                jQuery('#home').animate({ left: -rotateWidth + 'px' }, 1000);
                jQuery('#collaborate').animate({ left: '0' }, 1000);
                //$("#reglogin").hide();
                jQuery('.main a#collaborate-header').addClass('active');

                startAdvertise();
             
            }
            if (www.indexOf('#developers') !== -1 || www.indexOf('?developers') !== -1) {
                jQuery('#home').animate({ left: rotateWidth + 'px' }, 1000);
                jQuery('#developers').animate({ left: '0' }, 1000);
                //$("#reglogin").show();
                jQuery('.main a#developer-header').addClass('active');
            }
        }, 200);
        jQuery('a.logo').live('click', function () {

            History.pushState({ state: 3 }, "StartApp", "?home");
            return false;
        });

        jQuery('#footer-home').live('click', function () {
            jQuery('.main a').removeClass('active');
            History.pushState({ state: 3 }, "StartApp", "?home");
            return false;
        });

        jQuery('.main .animate').live('click', function () {


            jQuery('.main a').removeClass('active');
            var id = jQuery(this).attr('href');

            jQuery(this).addClass('active');
            if (id.indexOf('?advertisers') > -1) {

                History.pushState({ state: 1 }, "StartApp", "?advertisers");

            } else if (id.indexOf('?developers') > -1) {

                History.pushState({ state: 2 }, "StartApp", "?developers");
            }

            return false;
        });

        jQuery('.box a').live('click', function () {
            var id = jQuery(this).attr('href');
            if (id == '?advertisers') {

                History.pushState({ state: 1 }, "StartApp", "?advertisers");
                //$("#reglogin").hide();
                //jQuery('#home').animate({left:-rotateWidth+'px'}, 1000);
                //jQuery(id).animate({left:'0'}, 1000);
                jQuery('.main a').removeClass('active');
                jQuery('.main a[href="/?advertisers"]').addClass('active');
                //startAdvertise();
            } else {
                //$("#reglogin").show();
                History.pushState({ state: 2 }, "StartApp", "?developers");
                //jQuery('#home').animate({left:rotateWidth+'px'}, 1000);
                //jQuery(id).animate({left:'0'}, 1000);
                jQuery('.main a').removeClass('active');
                jQuery('.main a[href="/?developers"]').addClass('active');
            }

            return false;
        });
    }

});



function animateCollborate() {


    if (jQuery('#home').css('left') == '0px') {
        jQuery('#home').animate({ left: -rotateWidth + 'px' }, 1000);
    }
    //$("#reglogin").hide();
    jQuery('#collaborate').animate({ left: '0' }, 1000);
    jQuery('#developers').animate({ left: -rotateWidth + 'px' }, 1000);
    jQuery('.main a').removeClass('active');
    jQuery('.main a[href="/?advertisers"]').addClass('active');
    startAdvertise();
}
function animateDevelopers(id) {
    developersMenuIterationFreeze = 0;
    $("#reglogin").show();
    if (jQuery('#home').css('left') == '0px') {
        jQuery('#home').animate({ left: rotateWidth + 'px' }, 1000);
    }
    jQuery('#developers').animate({ left: '0' }, 1000);
    jQuery('#collaborate').animate({ left: rotateWidth + 'px' }, 1000);
    jQuery('.main a').removeClass('active');
    jQuery('.main a[href="/?developers"]').addClass('active');



}
function animateHome() {

    jQuery('.main a').removeClass('active');
    if (jQuery('#collaborate').css('left') == '0px') {
        jQuery('#home').animate({ left: '0' }, 1000);
        $("#reglogin").show();
        jQuery('#collaborate').animate({ left: rotateWidth + 'px' }, 1000);
    }

    if (jQuery('#developers').css('left') == '0px' /*|| jQuery('#developers').css('right') == '0px'*/) {
        $("#reglogin").show();
        jQuery('#home').animate({ left: '0' }, 1000);
        jQuery('#developers').animate({ left: -rotateWidth + 'px' }, 1000);
    }




}




function setPhoneBackgroundImage(sender) {
    jQuery('img.hand-image-developers').fadeOut("fast", function () {
        var image = $('img.hand-image-developers').attr('src', $(sender).attr('rel-url'));
        $('img.hand-image-developers').fadeIn("slow");

    });
    if ($(sender).attr('is-video-item') == '1') {
        $('.play-button').show("fast");

        $('img.hand-image-developers ,.play-button').unbind('click');
        $('img.hand-image-developers,.play-button').bind('click', function (e) {
         
            showVideoPlayer($(sender).attr('video-url'));
        });

    }
    else {
        $('.play-button').hide();
        $('img.hand-image-developers ,.play-button').unbind('click');

    }
};
 
     function showVideoPlayer(videoUrl) {
    developersMenuIterationFreeze = 1;
    if ($('#video-player').length > 0) {
        $('.video-player-container').css('left', '20%')
        videoPlayer = videojs("video-player");
        if (videoUrl.indexOf('.swf') > -1) {
            alert('flash video');
            videoPlayer.src({ type: "video", src: videoUrl.substring(1) });
        }
        else if (videoUrl.indexOf('.mp4') > -1) {


            videoPlayer.src({ type: "video/mp4", src: videoUrl.substring(1) });//videoUrl.substring(1) });   'media/27567/search-icon.mp4'
        }

        $('#modalBackground').show();
        $('.video-player-container').show();

        $('#modalBackground').unbind('click');
        $('#modalBackground').bind('click', function () {
            closeVideoPlayer();

        });

    }

};
  
 

function showVideoPlayerAdvertisers(videoUrl) {
  
    if ($('#video-player').length > 0) {
        $('.video-player-container').css('left', '220%')
        videoPlayer = videojs("video-player");
        if (videoUrl.indexOf('.swf') > -1) {
            alert('flash video');
            videoPlayer.src({ type: "video", src: videoUrl.substring(1) });
        }
        else if (videoUrl.indexOf('.mp4') > -1) {


            videoPlayer.src({ type: "video/mp4", src: videoUrl.substring(1) });//videoUrl.substring(1) });   'media/27567/search-icon.mp4'
        }

        $('#modalBackground').show();
        $('.video-player-container').show();

        $('#modalBackground').unbind('click');
        $('#modalBackground').bind('click', function () {
            closeVideoPlayer();

        });

    }

};


function closeVideoPlayer() {

    if ($('#video-player').length > 0) {

        videoPlayer = videojs("video-player");
        videoPlayer.pause();
    }

    $('#modalBackground').hide();
    $('.video-player-container').hide();
    developersMenuIterationFreeze = 0;
};

var currentIndex = 0;
function initDevelopersSection() {
    developersTimerId = window.setInterval(function () {

        if (developersMenuIterationFreeze == 0) {


            var sender;
            if ($('#developers .dev-box .select li.hover').length > 0) {
                currentIndex = parseInt($('#developers .dev-box .select li.hover').attr('item-index'));
                $('#developers .dev-box .select li.hover').removeClass('hover');
            }

            //Init CurrentIndex to 0
            if (currentIndex == $('#developers .dev-box .select li').length - 1) {
                currentIndex = -1;
            }

            sender = $("#developers .dev-box .select li[item-index=\"" + ++currentIndex + "\"]").addClass('hover');


            //switch (currentIndex) {
            //    case 0: {
            //        sender = $('#developers .dev-box .select li[item-index="1"]').addClass('hover');
            //    } break;
            //    case 1: {
            //        sender = $('.developers-menu .buttons  .button[item-index="2"]').addClass('hover');
            //    } break;
            //    case 2: {
            //        sender = $('#developers .dev-box .select li[item-index="3"]').addClass('hover');
            //    } break;

            //    case 3: {
            //        sender = $('.developers-menu .buttons  .button[item-index="4"]').addClass('hover');
            //    } break;

            //    case 4: {
            //        sender = $('.developers-menu .buttons  .button[item-index="0"]').addClass('hover');
            //    } break;


            //}

            setPhoneBackgroundImage(sender);

        }

    }, ad_image_rate);
}

function checkDeviceResolution(){

	var deviceWidth = Math.max(window.screen.width,window.screen.height);
	var deviceHeight = Math.min(window.screen.width,window.screen.height);
	$(".pageParams").attr("width",deviceWidth).attr("height",deviceHeight);
	if (deviceWidth < desktopMinWidth) {
		redirectToMobile();
	}		
}
function redirectToMobile(){
	document.location.href = 'http://m.startapp.com';
}